<?php
/*
 * 关于VO权限的定义
 *   备注
        团队创建者与项目创建者默认具有所有权限
        项目事务的任务，TODO:待完善
 *   团队事务
        修改团队基本信息
        修改团队项目信息
        成员邀请权限
        决定本部门成员加入权限
        辞退本部门成员权限
        添加部门和职位权限
        删除部门和职位权限
 *   项目事务
        创建任务卡片权限
        删除任务权限
        添加任务权限
        修改所有任务状态(可修改所有任务的状态)
        所有任务管理权限
        删除任务卡片权限
        任务内容(内容、执行者、截止期限等)修改权限
 */


class TeamBaseAction extends CommonAction {
    public function __construct() {
        parent::__construct();
        if ($this->dtpass) {
            
        }
        else {
            //是否已激活账户
            $account = M('account')->where('uid='.$this->uid)->find();
            if ($account['emailverify']==0) {
                $this->redirect("Login/emailverify@".getDomain(), array('code'=>0));
            }

            //校验是否已退出团队
            $juTab = M('job_user');
            $resJobuser = $juTab->where('uid='.$this->uid.' and state=4')->find();
            if($resJobuser){
                if(time()-$resJobuser['uptime']>C('APPLY_TIMEOUT')){//超三天期限
                    $juTab->where('uid='.$this->uid.' and state=4')->delete();
                    $data = array(
                        'teamname' => '',
                        'teamid'=> '-1',
                        'projectname' => '',
                        'projectid' => '-1',
                    );
                    $ret = M('user_info')->where('uid='.$this->uid)->save($data);
                    if ($ret) {
                        $this->userinfo['teamname'] = '';
                        $this->userinfo['projectname'] = '';
                        $this->userinfo['teamid'] = -1;
                        $this->userinfo['projectid'] = -1;
                    }
                }
            }
        }
    }
    
    //是否团队创建者
    //参数列表: $id 团队id
    public function isTeamCreater($id) {
        $resTeam = M('team')->where('id='.$id.' and state>0')->find();
        if ($resTeam['uid']==$this->uid) {
            return $resTeam;
        }
        return false;
    }
    
    //是否项目创建者
    //参数列表：$id 项目id
    public function isProjectCreater($id) {
        $resPrj = M('project')->where('id='.$id.' and state>0')->find();
        if ($resPrj['uid']==$this->uid){//项目创建者
            return $resPrj;
        }
        return false;
    }
    
    //是否有权限
    //参数列表： $uid 用户id
    //          $authid 权限id,字符串，多个值可写成id1,id2...
    public function teamauthcheck($uid, $authid) {
        $resJobuser = M('job_user')->where('uid='.$uid.' and state=2')->find();
        if (!$resJobuser) {
            return false;
        }
        $ret = M('authdispatch')->where('arid='.$resJobuser['id'].' and authid in ('.$authid.')')->find();
        if ($ret) {
            return $resJobuser;
        }
        return false;
    }
    
    //统计数据header
    public function dataheader() {
        $statistic = array(
            'team' => '-',
            'prj' => '-',
        );
        if ($this->userinfo['teamid']>0) {
            $statistic = array(
                'team' => $this->userinfo['teamname'],
                'prj' => $this->userinfo['projectname'],
            );
        }
        
        $statistic['jname'] = '-';
        $resJu = M('job_user')->table(array('xia_job_user'=>'ju','xia_job'=>'job'))->field('job.name as jname')->where('ju.uid='.$this->uid.' and ju.state in (2,4,5) and ju.jobid=job.id and job.state>0')->find();
        if ($resJu) {
            $statistic['jname'] = $resJu['jname'];
        }
        else{
            $resTeam = M('team')->where('id='.$this->userinfo['teamid'].' and state>0')->find();
            if ($resTeam['uid']==$this->uid) {
                $statistic['jname'] = '创始人';
            }
        }
        $this->assign('statistic', $statistic);
    }
}

?>